﻿using System;
using System.Linq;
using System.Windows;
using HappyBa.Core;
using HappyBa.Core.Data;
using HappyBa.Core.Interfaces;
using HappyBa.Localization;
using Mnk.Library.Common.Log;
using Mnk.Library.WpfControls.Tools;

namespace HappyBa.Pages
{
    /// <summary>
    /// Interaction logic for SettingsPage.xaml
    /// </summary>
    public partial class SettingsPage
    {
        private readonly ILog log = LogManager.GetLogger<SettingsPage>();
        private readonly IDataCache dataCache;
        private readonly Config config;

        public SettingsPage()
        {
            InitializeComponent();
            var container = ((MainWindow)Application.Current.MainWindow).Container;
            container.GetInstance<IDataCollector>().OnDataChanged += (o, e) => OnDataContextChanged(o, new DependencyPropertyChangedEventArgs());
            DataContextChanged += OnDataContextChanged;

            dataCache = container.GetInstance<IDataCache>();
            config = container.GetInstance<Config>();
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            Projects.OnCheckChangedEvent(sender,e);
        }

        private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (config.Current == null) return;

            try
            {
                var search = new MetricsSearch(config.Current.Search);
                var metrics = new Metrics(dataCache.Metrics.Data, search);
                ActiveEmployees.ConfigureInputTextList(HappyBaLang.ManageEmployees, config.Current.ActiveEmployees, metrics.AllUsers.Select(x => x.ToString()).ToArray(), isReadOnly: true);
            }
            catch (Exception ex)
            {
                log.Write(ex, "Can't configure settings page");
            }
        }
    }
}
